<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta
            name="viewport"
            content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
    />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js"></script>
    <script
            src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"
            integrity="sha512-iKDtgDyTHjAitUDdLljGhenhPwrbBfqTKWO1mkhSFH3A7blITC9MhYon6SjnMhp4o0rADGw9yAC6EW4t5a4K3g=="
            crossorigin="anonymous"
            referrerpolicy="no-referrer"
    ></script>
</head>
<body>
<h3>聊天界面</h3>
<div>
    <label for="name">用户:</label>
    <input type="text" id="name" />
</div>
<div>
    <button>连接</button>
    <button>断开</button>
</div>

<div>
    <label for="content">内容:</label>
    <input type="text" id="content" />
</div>

<button id="send">发送</button>
<script>
    var socket = new SockJS("/chat");
    var stompClient = Stomp.over(socket);

    stompClient.connect({}, function(frame) {
        stompClient.subscribe("/topic/greetings", function(greeting){
            console.log("数据接收;");
            console.log(JSON.stringify(greeting.body))
        })
    });

    document.getElementById("send").onclick = function(){
        stompClient.send("/app/hello", {},JSON.stringify({
            name: "王大锤",
            time: "2020-12-20",
            content: "What's your name"
        }))
    }
</script>
</body>
</html>
